package controller;

import net.sf.json.JSONObject;
import pojo.Cate;
import pojo.User;
import service.CateManagement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author: zen
 * @date: 2020/4/17
 * @Title: ShareCate
 * @Description: 处理用户分享美食
 */

@WebServlet("/shareCate")
public class ShareCate extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        String cate = req.getParameter("cate");
        System.out.println("===============分享美食===============");
        System.out.println("JSON格式：" + cate.toString());
        System.out.println("原生JSON：" + cate);
        System.out.println("===============分享美食===============");
        JSONObject jsonObject = JSONObject.fromObject(cate);
        System.out.println(jsonObject);
        Cate cateInfo = (Cate) JSONObject.toBean(jsonObject, Cate.class);
        System.out.println("++++++++++++++++++测试是否打印++++++++++++++++++");
        System.out.println(cateInfo.getLevel());
        System.out.println(cateInfo.toString());
        if (CateManagement.uploadCate(cateInfo)) {
            System.out.println("成功执行了添加美食");
            out.write("ok");
        } else {
            System.out.println("添加美食失败！！");
            out.write("error");
        }
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
